from django.conf.urls.defaults import *
from django.views.generic import list_detail
from django.conf import settings
from django.contrib.auth.decorators import permission_required
from django.views.generic import create_update

import browsertests.results.views
import browsertests.results as results
from browsertests.results.models import Result

PAGINATE_BY = 100

result_list = {
    'queryset': Result.objects.order_by('-date').select_related(),
    'template_object_name': 'result',
    'paginate_by': PAGINATE_BY,
}

result_detail = {
    'queryset': Result.objects.select_related(),
    'template_object_name': 'result',
    'extra_context': {'REFTEST_RESULTS_URL': settings.REFTEST_RESULTS_URL},
}

result_delete = {
    'model': Result,
    # XXX reverse() can't be used here because urlpatterns aren't parsed yet.
    #'post_delete_redirect': reverse('result-list'),
    'post_delete_redirect': '/',
}

def result_list_bysession(request, session, **kwargs):
    kwargs['queryset'] = Result.objects.order_by('-date').select_related() \
                               .filter(session=session)
    return list_detail.object_list(request, **kwargs)


urlpatterns = patterns('',
    url(
        regex   = '^page(?P<page>\d+)/$',
        view    = list_detail.object_list,
        kwargs  = result_list,
        name    = 'result-list',
    ),
    url(
        regex   = '^(?P<object_id>\d+)/$',
        view    = list_detail.object_detail,
        kwargs  = result_detail,
        name    = 'result-detail',
    ),
    url(
        regex   = '^(?P<object_id>\d+)/delete/$',
        view    = permission_required('browsertests.delete_result')
                                     (create_update.delete_object),
        kwargs  = result_delete,
        name    = 'result-delete',
    ),
    url(
        regex   = '^bysession/(?P<session>\w+)/page(?P<page>\d+)/$',
        view    = result_list_bysession,
        kwargs  = result_list,
        name    = 'result-list-bysession',
    ),
    url(
        regex   = '^search/$',
        view    = results.views.result_search,
        name    = 'result-search',
    ),
    url(
        regex   = '^compare_ua/$',
        view    = results.views.compare_ua,
        name    = 'result-compare-ua',
    ),
)
